package cn.bearspark.forward.provider.dao.buffer;

import cn.bearspark.forward.provider.dao.key.HappeningFwdBufferKeyBuilder;
import cn.bearspark.forward.provider.dao.mapper.HappeningFwdMapper;
import cn.bearspark.framework.redis.starter.buffer.list.RedisPageListBuffer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 转发动态的用户 id 集合的缓存器
 *
 * @author f
 */
@Component
public class FwdHappeningUserIdPageListBuffer extends RedisPageListBuffer {

    private final HappeningFwdMapper happeningFwdMapper;

    public FwdHappeningUserIdPageListBuffer(RedisTemplate<String, Object> redisTemplate,
                                            HappeningFwdMapper happeningFwdMapper,
                                            HappeningFwdBufferKeyBuilder happeningFwdBufferKeyBuilder) {
        super(
                redisTemplate,
                happeningFwdMapper,
                happeningFwdBufferKeyBuilder
        );
        this.happeningFwdMapper = happeningFwdMapper;
    }

    /**
     * 判断指定用户是否转发过指定动态
     *
     * @param userId      指定的用户 id
     * @param happeningId 指定的动态 id
     * @return 指定用户是否转发过指定动态
     */
    public boolean isForwarded(long userId, long happeningId) {
        return happeningFwdMapper.count(userId, happeningId) != 0;
    }
}
